/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package heartsinjava2;
import java.util.Random;
import javax.swing.*;


/**
 * A basic extension of the javax.swing.JApplet class
 */
public class CardDeckDemo extends JApplet
{

    private Deck cardDeck;
    private final String directory = "cards\\";

    javax.swing.JButton cardDisplayLabel = new javax.swing.JButton();
    javax.swing.JButton drawCardBtn = new javax.swing.JButton();
    javax.swing.JButton newDeckBtn = new javax.swing.JButton();
    javax.swing.JLabel cardsLeftLbl = new javax.swing.JLabel();

	public void init()
	{
		// Take out this line if you don't use symantec.itools.net.RelativeURL or symantec.itools.awt.util.StatusScroller
		//symantec.itools.lang.Context.setApplet(this);

		// This line prevents the "Swing: checked access to system event queue" message seen in some browsers.
		getRootPane().putClientProperty("defeatSystemEventQueueCheck", Boolean.TRUE);

		// This code is automatically generated by Visual Cafe when you add
		// components to the visual environment. It instantiates and initializes
		// the components. To modify the code, only use code syntax that matches
		// what Visual Cafe can generate, or Visual Cafe may be unable to back
		// parse your Java file into its visual environment.
		//{{INIT_CONTROLS
		getContentPane().setLayout(null);
		getContentPane().setBackground(java.awt.Color.cyan);
		setSize(236,333);


		cardDisplayLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
		cardDisplayLabel.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
		cardDisplayLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		//cardDisplayLabel.setText("Draw a card...");
		cardDisplayLabel.setOpaque(true);
		getContentPane().add(cardDisplayLabel);
		cardDisplayLabel.setBackground(java.awt.Color.lightGray);
		cardDisplayLabel.setForeground(java.awt.Color.black);
		cardDisplayLabel.setBounds(48,24,73,97);


		drawCardBtn.setText("Draw a card...");
		getContentPane().add(drawCardBtn);
		drawCardBtn.setBounds(24,204,192,24);

		newDeckBtn.setText("New & shuffled deck...");
		getContentPane().add(newDeckBtn);
		newDeckBtn.setBounds(24,240,192,24);

		cardsLeftLbl.setText("Cards Left:");
		getContentPane().add(cardsLeftLbl);
		cardsLeftLbl.setForeground(java.awt.Color.black);
		cardsLeftLbl.setBounds(24,276,193,24);

		//}}

		cardDeck = new Deck();


     



		cardsLeftLbl.setText( "Cards Left: " + cardDeck.size() );

		//{{REGISTER_LISTENERS
		SymAction lSymAction = new SymAction();
		drawCardBtn.addActionListener(lSymAction);
		newDeckBtn.addActionListener(lSymAction);
		//}}
	}

	//{{DECLARE_CONTROLS

	//}}

	class SymAction implements java.awt.event.ActionListener
	{
		public void actionPerformed(java.awt.event.ActionEvent event)
		{
			Object object = event.getSource();
			if (object == drawCardBtn)
				drawCardBtn_actionPerformed(event);
			else if (object == newDeckBtn)
				newDeckBtn_actionPerformed(event);
		}
	}

	void drawCardBtn_actionPerformed(java.awt.event.ActionEvent event)
	{
		Card card = cardDeck.draw();
		//cardDisplayLabel.setText( card.toString() );
       		cardDisplayLabel.setIcon(card.getCardImage());
		cardsLeftLbl.setText( "Cards Left: " + cardDeck.size() );
		if ( cardDeck.isEmpty() )
		    drawCardBtn.setEnabled( false );
	}



	void newDeckBtn_actionPerformed(java.awt.event.ActionEvent event)
	{
		//cardDeck.restoreDeck();
                cardDeck = new Deck();
                Random rng = new Random();
		cardDeck.shuffle(rng);
		drawCardBtn.setEnabled( true );
		cardDisplayLabel.setIcon( null );
		//cardDisplayLabel.setText( "Draw a card..." );
		cardsLeftLbl.setText( "Cards Left: " + cardDeck.size() );
	}


}
